package com.examstack.portal.security.handler;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.AbstractAuthenticationTargetUrlRequestHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;

/**
 * <tt>AuthenticationSuccessHandler</tt> which can be configured with a default URL which users should be
 * sent to upon successful authentication.
 * <p>
 * The logic used is that of the {@link AbstractAuthenticationTargetUrlRequestHandler parent class}.
 *
 * @author Ocelot
 * @since 3.0
 */
public class ExtrAuthenticationSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler implements AuthenticationSuccessHandler {
   private String mobileDefaultTargetUrl;
   private String targetUrl;
    public ExtrAuthenticationSuccessHandler() {
    }

    /**
     * Constructor which sets the <tt>defaultTargetUrl</tt> property of the base class.
     * @param targetUrl the URL to which the user should be redirected on successful authentication.
     */
    public ExtrAuthenticationSuccessHandler(String targetUrl) {
        //super..();
        setDefaultTargetUrl(targetUrl);
    }

    /**
     * Calls the parent class {@code handle()} method to forward or redirect to the target URL, and
     * then calls {@code clearAuthenticationAttributes()} to remove any leftover session data.
     */
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {
        /*Object mobile = request.getSession().getAttribute("isMobile");
        if(null!=mobile){
            String m =mobile.toString();
            if(m.equals("true")){
                String url = "/mobile/mobile-home";
                setDefaultTargetUrl(url);
                //request.getSession().setAttribute("isMobile",false);
            }
        }else{
            setDefaultTargetUrl("/home");
        }*/
        if(request.getHeader("referer").contains("mobile")){
            setDefaultTargetUrl(mobileDefaultTargetUrl);
        }else{
            setDefaultTargetUrl(targetUrl);
        }

            handle(request, response, authentication);

//      String language = request.getParameter("j_language");
//		request.getSession().setAttribute("language", language);
        clearAuthenticationAttributes(request);
    }

    /**
     * Removes temporary authentication-related data which may have been stored in the session
     * during the authentication process.
     */
    protected final void clearAuthenticationAttributes(HttpServletRequest request) {
        HttpSession session = request.getSession(false);

        if (session == null) {
            return;
        }

        session.removeAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
    }

    public String getMobileDefaultTargetUrl() {
        return mobileDefaultTargetUrl;
    }

    public void setMobileDefaultTargetUrl(String mobileDefaultTargetUrl) {
        this.mobileDefaultTargetUrl = mobileDefaultTargetUrl;
    }

    public String getTargetUrl() {
        return targetUrl;
    }

    public void setTargetUrl(String targetUrl) {
        this.targetUrl = targetUrl;
    }
}
